using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Configuration.Install;
using FSHelperLib;

namespace InstallWebConfig
{
    [RunInstaller(true)]
    public class WebConfigInstaller : Installer
    {

        public WebConfigInstaller()
            : base()
        {
        }

        private string GetConfigFileName(Assembly ExecutingAssembly)
        {
            string sPath = "";
            { //root folder one level up from bin
                sPath = SystemHelper.GetWebRootFolder(ExecutingAssembly);
                sPath = sPath + @"\web.config";
            }
            if (!File.Exists(sPath))
            {
                throw new FileNotFoundException("Missing config file " + sPath);
            }
            return sPath;
        }

        protected void updateConfigFile(string dbname, string hostname, string dbtype, string uid, string pwd)
        {
            string connString;
            string invconnStr;
            string connStrFileName;

            connStrFileName = GetConfigFileName(Assembly.GetExecutingAssembly());
            
            //Case MySQL
            connString = "DRIVER={MySQL ODBC 3.51 Driver};server=" + hostname + ";database=" + dbname + ";option=3;port=3306;uid=" + uid + ";pwd=" + pwd;
            invconnStr = "Server=" + hostname + ";Uid=" + uid + ";Pwd=" + pwd + "; Database = " + dbname;
            // Open the web.config for reading
            
 //WRITE THE DAL Connection String
            
            StreamReader streamReader;
            streamReader = File.OpenText(connStrFileName);
            
            // Now, read the entire file into a string
            string contents = streamReader.ReadToEnd();
            streamReader.Close();

            // Write the modification into the same file
            StreamWriter streamWriter=File.CreateText(connStrFileName);

            streamWriter.Write(contents.Replace("[CONNSTRING]", connString));
            streamWriter.Close();        

//WRITE THE INV Connection String
            
            StreamReader streamReader2;
            streamReader2 = File.OpenText(connStrFileName);
            
            // Now, read the entire file into a string
            string contents2 = streamReader2.ReadToEnd();
            streamReader2.Close();

            // Write the modification into the same file
            StreamWriter streamWriter2 =File.CreateText(connStrFileName);

            streamWriter2.Write(contents2.Replace("[INVCONNSTRING]", invconnStr));
            streamWriter2.Close();        
        }
        
        public override void Install(IDictionary savedState)
        {
            base.Install(savedState);
            updateConfigFile(this.Context.Parameters["dbname"], this.Context.Parameters["hostname"], this.Context.Parameters["dbType"], this.Context.Parameters["uid"], this.Context.Parameters["pwd"]);
        }
        
    }

}
